/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhihu.keyword.service.impl;

import me.zhengjie.modules.zhihu.keyword.domain.ZhKeyword;
import me.zhengjie.modules.zhihu.theme.service.dto.ZhThemeDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhihu.keyword.repository.ZhKeywordRepository;
import me.zhengjie.modules.zhihu.keyword.service.ZhKeywordService;
import me.zhengjie.modules.zhihu.keyword.service.dto.ZhKeywordDto;
import me.zhengjie.modules.zhihu.keyword.service.dto.ZhKeywordQueryCriteria;
import me.zhengjie.modules.zhihu.keyword.service.mapstruct.ZhKeywordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王超
* @date 2022-01-04
**/
@Service
@RequiredArgsConstructor
public class ZhKeywordServiceImpl implements ZhKeywordService {

    private final ZhKeywordRepository zhKeywordRepository;
    private final ZhKeywordMapper zhKeywordMapper;

    @Override
    public Map<String,Object> queryAll(ZhKeywordQueryCriteria criteria, Pageable pageable){
        Page<ZhKeyword> page = zhKeywordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhKeywordMapper::toDto));
    }

    @Override
    public List<ZhKeywordDto> queryAll(ZhKeywordQueryCriteria criteria){
        return zhKeywordMapper.toDto(zhKeywordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhKeywordDto findById(String id) {
        ZhKeyword zhKeyword = zhKeywordRepository.findById(id).orElseGet(ZhKeyword::new);
        ValidationUtil.isNull(zhKeyword.getId(),"ZhKeyword","id",id);
        return zhKeywordMapper.toDto(zhKeyword);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhKeywordDto create(ZhKeyword resources) {
        resources.setId(IdUtil.simpleUUID());
        return zhKeywordMapper.toDto(zhKeywordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhKeyword resources) {
        ZhKeyword zhKeyword = zhKeywordRepository.findById(resources.getId()).orElseGet(ZhKeyword::new);
        ValidationUtil.isNull( zhKeyword.getId(),"ZhKeyword","id",resources.getId());
        zhKeyword.copy(resources);
        zhKeywordRepository.save(zhKeyword);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            zhKeywordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhKeywordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhKeywordDto zhKeyword : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("关键词名称", zhKeyword.getKeyName());
            map.put(" isDeleted",  zhKeyword.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String, String>> getKeywordDict(List<ZhKeywordDto> all) throws IOException {
        List<Map<String, String>> list = new ArrayList<>();
        for (ZhKeywordDto zh : all) {
            Map<String,String> map = new LinkedHashMap<>();
            map.put("name", zh.getKeyName());
            map.put("code", zh.getId());
            list.add(map);
        }
        return list;
    }
}
